package com.longrise.android.byjk.plugins.vip.integralexchange;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;



import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;

/**
 * Created by callxkj on 2017/7/25.
 * 我的勋章
 */

public class MyMedalActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,
        View.OnClickListener, OnIntegralExchangeRcvItemClickListener {

    private Toolbar mToolbar;
    private RelativeLayout mRlempty;
    private TextView mTvexchange;
    private RelativeLayout mRlrcv;
    private BBswipeRefreshLayout mBbsrl;
    private RecyclerView mRcv;
    private IExchangezyxxzAdapter mAdapter;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_mymedal;
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle(AppUtil.getString(R.string.mymedal_title));
        setSupportActionBar(mToolbar);

        mRlempty = (RelativeLayout) findViewById(R.id.mymedal_empty_rl);
        mTvexchange = (TextView) findViewById(R.id.mymedal_empty_t2);
        mRlrcv = (RelativeLayout) findViewById(R.id.mymedal_empty_rcv_rl);
        mBbsrl = (BBswipeRefreshLayout) findViewById(R.id.mymedal_empty_srl);
        mRcv = (RecyclerView) findViewById(R.id.mymedal_empty_rcv);

        initRcv();
        initEvent();
        addScreenName("我的勋章");
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData(false);
    }

    private void initRcv() {
        LinearLayout headView = new LinearLayout(mContext);
        headView.setBackgroundColor(Color.parseColor("#FFFFFF"));
        int height = AppUtil.dip2px(12);
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width,height);
        headView.setLayoutParams(layoutParams);
        mAdapter = new IExchangezyxxzAdapter("我的勋章");
        mAdapter.addHeaderView(headView);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2, GridLayoutManager.VERTICAL, false);
        mRcv.setLayoutManager(layoutManager);
        mRcv.setAdapter(mAdapter);
        mAdapter.setOnRcvItemClickListener(this);
    }

    private void initEvent() {
        mBbsrl.setOnRefreshListener(this);
        mTvexchange.setOnClickListener(this);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 加载数据需要展示的数据
     * @param flag 是否用swipeRefreshLayout
     */
    private void initData(final boolean flag) {
        mBbsrl.setRefreshing(flag);
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        if(!flag){
            DZZWTools.showWaitDialog2(MyMedalActivity.this);
        }
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_sMyMedalInfo", fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            String resultdesc = bean.getString("resultdesc");
                            int value = (Integer) bean.get("resultstate");
                            if (value == 1) {
                                EntityBean[] results = bean.getBeans("result");
                                parseResult(results);

                                MyMedalRankHelper.reloadRank(MyMedalActivity.this,!flag);
                            } else {
                                showToast(resultdesc);
                                DZZWTools.dismissDialog(null);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            DZZWTools.dismissDialog(null);
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        refreshComplete();
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        DZZWTools.dismissDialog(null);
                    }
                });
    }

    private void parseResult(EntityBean[] results) {
        if (results == null || results.length == 0) {
            mRlrcv.setVisibility(View.GONE);
            mRlempty.setVisibility(View.VISIBLE);
        }else{
            mRlrcv.setVisibility(View.VISIBLE);
            mRlempty.setVisibility(View.GONE);
            mAdapter.setDatas(results, null);
        }

    }

    /**
     * 使用或者取消勋章
     */
    private void toUseMedal(String medalid, String medalstate) {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        bean.set("medalid", medalid);
        bean.set("medalstate", medalstate);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        DZZWTools.showWaitDialog2(MyMedalActivity.this);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_uVIPstateByMedalid", fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            String resultdesc = bean.getString("resultdesc");
                            int resultstate = (Integer) bean.get("resultstate");
                            if (resultstate == 1) {
                                initData(false);
                            }else{
                                DZZWTools.dismissDialog(null);
                            }
                            showToast(resultdesc);
                        } catch (Exception e) {
                            e.printStackTrace();
                            DZZWTools.dismissDialog(null);
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {

                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        DZZWTools.dismissDialog(null);
                    }
                });
    }



    /**
     * rcv 下拉刷新完成
     */
    private void refreshComplete() {
        mBbsrl.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        initData(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mymedal_empty_t2:
                Bundle bundle = new Bundle();
                bundle.putInt(IntegralExchange2Activity.PAGE_FROM_FLAG,IntegralExchange2Activity.PAGE_FROM_MYMEDAL);
                startActivity(IntegralExchange2Activity.class,bundle);
                addGoogleEvent("我的勋章","立即兑换",null);
                break;
        }
    }

    @Override
    public void onClick(int type, String lockstate, String goodsid, String goodstype, String needscores, String imageUrl, String detailpicurl, int position) {
        if("1".equals(lockstate) || "2".equals(lockstate)){
            //1:已解锁 在使用  0：未解锁 2:已解锁 未使用 3.已解锁 商品到期
            //在我的勋章页面应该只能有
            toUseMedal(goodsid, lockstate);
        }

    }

    private void addScreenName(String name) {
        Tracker_Google.getInstance().addGoogleScreenName(name);
    }

    private void addGoogleEvent(String par1, String par2, String par3) {
        Tracker_Google.getInstance().addEvent(par1, par2, par3);
    }
}
